package com.shmilyou.dissertationmanagement.biz;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/9 */

import com.shmilyou.dissertationmanagement.domain.Thesis;
import com.shmilyou.dissertationmanagement.domain.ThesisEx;
import com.shmilyou.dissertationmanagement.repository.ThesisExRepository;
import com.shmilyou.dissertationmanagement.repository.ThesisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ThesisService {

    private final ThesisRepository thesisRepository;
    private final ThesisExRepository thesisExRepository;

    @Autowired
    public ThesisService(ThesisRepository thesisRepository,
                         ThesisExRepository thesisExRepository) {
        this.thesisRepository = thesisRepository;
        this.thesisExRepository = thesisExRepository;
    }

    public Thesis queryById(Integer id) {
        Optional<Thesis> thesis = thesisRepository.findById(id);
        return thesis.orElse(null);
    }

    public Thesis save(Thesis entity) {
        return thesisRepository.save(entity);
    }

    public Thesis update(Thesis entity) {
        return thesisRepository.save(entity);
    }

    public Thesis delete(Thesis entity) {
        entity.setDel(1);
        return thesisRepository.save(entity);
    }

    public List<Thesis> queryByName(String name) {
        return thesisRepository.findAllByName(name);
    }

    public List<Thesis> queryAll() {
        return thesisRepository.findAllByDel(0);
    }

    public Page<Thesis> queryAll(int pageIndex, int pageSize) {
        Pageable page = PageRequest.of(pageIndex, pageSize);
        Page<Thesis> content = thesisRepository.findAllByDel(page, 0);
        return content;
    }

    public ThesisEx saveThesisEx(ThesisEx entity) {
        return thesisExRepository.save(entity);
    }

    public List<ThesisEx> queryExById(List<Integer> id) {
        return thesisExRepository.findAllByIdIn(id);
    }

    public ThesisEx queryExById(Integer id) {
        Optional<ThesisEx> byId = thesisExRepository.findById(id);
        return byId.orElse(new ThesisEx());
    }

    public int getCount() {
        return thesisRepository.countByDel(0);
    }
}
